var businessT = require('../../../../tool/business-tool.js')
var url = require('../../../../constant/url.js')
import Net from '../../../../tool/net.js'
var net = new Net()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 'add',
    data: { u_name: '', phone: '', area: '', address:'' },
    area: ''
  },

  formSubmit: businessT.action(function (sender) {
    businessT.debugLog(sender.detail)
    var data = sender.detail.value
    if (data.u_name == '') {
      wx.showToast({
        title: '请输入收货人姓名',
        icon: 'none'
      })
      return
    } else if (data.phone == '') {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return
    } else if (data.area == '') {
      wx.showToast({
        title: '请选择所在地区',
        icon: 'none'
      })
      return
    } else if (data.address == '') {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
      return
    } else {
      this.uploadData(data)
    }
  }),

  uploadData: function (data) {
    wx.showLoading({
      title: this.data.type == 'add' ? '正在添加' : '正在修改',
      mask: true
    })
    net.connectNeedLogin({
      url: url.app_host + url.addAndUpdateMailingAddress,
      data: data,
      success: (err, res) => {
        var pages = getCurrentPages()
        pages[pages.length - 2].reloadData(data)
        wx.hideLoading()
        wx.navigateBack({
          
        })
      },
      fail: (err, res) => {
        wx.hideLoading()
        businessT.showFailTips(err, this.data.type == 'add' ? '添加失败' : '修改失败')
      }
    })
  },

  pickerChange: businessT.action(function (sender) {
    businessT.debugLog(sender)
    var a = sender.detail.value
    var area = ''
    for (var i in a) {
      area = area + a[i]
    }

    this.data.area = area
    this.setData({
      area: this.data.area
    })
  }),

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.type = options.type
    if (this.data.type == 'edit') {
      wx.setNavigationBarTitle({
        title: '更改信息'
      })
      var pages = getCurrentPages()
      this.data.data = pages[pages.length - 2].data.data
      this.data.area = this.data.data.area
      this.setData({
        data: this.data.data,
        area: this.data.area
      })
    } else {
      wx.setNavigationBarTitle({
        title: '添加信息'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/page/home/index',
      imageUrl: '../../../../icon/public/share_img.png',
      title: '自助缴社保公积金，覆盖200多个城市，专业团队操作'
    }
  }
})